import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_book/config/config.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_book/route/route_config.dart';
import 'package:flutter_book/util/extensions/string_extensions.dart';
import 'package:get/get_navigation/src/root/get_material_app.dart';
import 'package:package_info/package_info.dart';
import 'package:permission_handler/permission_handler.dart';

import 'config/application.dart';
import 'config/localization_delegate.dart';
import 'config/shared_preferences.dart';
import 'data/database/db_helper.dart';

void main() {
  //设置状态栏透明
  SystemUiOverlayStyle systemUiOverlayStyle = SystemUiOverlayStyle(statusBarColor: Colors.transparent);
  SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
  _initApp().then((value) => runApp(MyApp()));
}



//初始化APP
Future _initApp() async {

  // 升级flutter版本1.10.2后，因为在main()方法中有异步操作，对一些插件做了初始化操作。
  //要先执行该方法 不然插件无法加载调用
  WidgetsFlutterBinding.ensureInitialized();

  // 强制竖屏
  SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]);

  //获取版本号
  PackageInfo.fromPlatform().then((PackageInfo packageInfo) {
    //app名称
    String appName = packageInfo.appName;
    //包名
    String packageName = packageInfo.packageName;
    //app版本
    String version = packageInfo.version;
    //build号
    String buildNumber = packageInfo.buildNumber;
    AppConfig.version = version;
    AppConfig.buildNumber = buildNumber;
    'appName==&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&=========$appName'.print;
    'packageName======&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&======$packageName'.print;
    'version=========&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&===$version'.print;
    'buildNumber=====&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&=======$buildNumber'.print;
  });

  //获取操作系统
  Application.platform = Platform.isAndroid ? 0 : Platform.isIOS ? 1 : -1;

  // 申请通知权限
  // 检查是否已有读写内存的权限
  bool status = await Permission.notification.isGranted;
  //判断如果还没拥有读写权限就申请获取权限
  if (!status) {
    await Permission.notification.request().isGranted;
  }

  //初始化SharedPreferences
  AppPrefs.init();

  //初始化数据库
  await DBHelper.instance.initDB();

  //创建各文件路径
  AppConfig.createAppDir();

}

class MyApp extends StatefulWidget {
  @override
  MyAppState createState() => MyAppState();
}

class MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    "Main_________________________________build".print;
    return GetMaterialApp(
      // title: 'Flutter Demo',
      theme: ThemeData(
        // primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
        platform: TargetPlatform.iOS,
      ),
      initialRoute: RouteConfig.main,
      getPages: RouteConfig.getPages,
      /*
       本地化的代理类
      */
      localizationsDelegates: [
        GlobalMaterialLocalizations.delegate, // 指定本地化的字符串和一些其他的值
        GlobalCupertinoLocalizations.delegate, // 对应的Cupertino风格
        GlobalWidgetsLocalizations.delegate, // 指定默认的文本排列方向, 由左到右或由右到左
        /// 注册我们的Delegate
        FZLocalizationDelegate.delegate
      ],
      supportedLocales: [
        const Locale('zh', 'CN'),
        const Locale('en', 'US'),
      ],

      /// 监听系统语言切换
      localeListResolutionCallback: (deviceLocale, supportedLocales) {
        'deviceLocale: $deviceLocale'.print;
        // 系统语言是英语： deviceLocale: [en_CN, en_CN, zh_Hans_CN]
        // 系统语言是中文： deviceLocale: [zh_CN, zh_Hans_CN, en_CN]
        'supportedLocales: $supportedLocales'.print;
      },
    );
  }

  @override
  void dispose() {
    "❌APP dispose！！！❌".print;
    DBHelper.instance.closeDB();

    super.dispose();
  }
}
